package com.wangxiang.shop.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.wangxiang.shop.product.dao.BaseTwoRelDao;
import com.wangxiang.shop.product.dao.ShopBaseDao;
import com.wangxiang.shop.product.entity.HttpAddEntity;
import com.wangxiang.shop.product.entity.ShopBaseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wangxiang.shop.product.entity.ShopTwoTypeEntity;
import com.wangxiang.shop.product.service.ShopTwoTypeService;
import com.wangxiang.common.utils.PageUtils;
import com.wangxiang.common.utils.R;



/**
 * 
 *
 * @author wangxiang
 * @email 2293672863@qq.com
 * @date 2022-03-04 11:20:08
 */
@RestController
@RequestMapping("product/shoptwotype")
public class ShopTwoTypeController {
    @Autowired
    private ShopTwoTypeService shopTwoTypeService;
    @Autowired
    private ShopBaseDao shopBaseDao;
    @Autowired
    private BaseTwoRelDao baseTwoRelDao;


    /**
     * 添加,设计到关系表的插入
     */
    @RequestMapping("/add")
    public R add(@RequestBody HttpAddEntity addEntity){

        if (addEntity.getName().equals("")){
            return R.error("请填写数据在添加");
        }
        //查询父级是否存在
        ShopBaseEntity shopBaseEntity = shopBaseDao.selectById(addEntity.getParentId());
        if (shopBaseEntity==null){
            return R.error("父级不存在");
        }
        List<Integer> childrenId = baseTwoRelDao.getChildrenId(addEntity.getParentId());
        boolean entity = shopTwoTypeService.getEntity(childrenId, addEntity.getName());
        if (!entity){
            return R.error("商品已存在");
        }


        return R.ok().put("msg",addEntity.getName()+"插入成功").put("code",600);

    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = shopTwoTypeService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		ShopTwoTypeEntity shopTwoType = shopTwoTypeService.getById(id);

        return R.ok().put("shopTwoType", shopTwoType);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ShopTwoTypeEntity shopTwoType){
		shopTwoTypeService.save(shopTwoType);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ShopTwoTypeEntity shopTwoType){
		shopTwoTypeService.updateById(shopTwoType);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		shopTwoTypeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
